<script>
import { mapGetters } from 'vuex'
import AreaForm from '@/views/car/area/components/areaForm/areaForm.vue'

export default {
  name: 'area',
  components: { AreaForm },
  data() {
    return {
      /* 获取区域列表的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 20
      },
      /* 是否显示添加或者编辑区域的弹框 */
      dialogVisible: false,
      /* 是否处于编辑状态 */
      edit: false,
      /* 是否处于loading状态 */
      loading: false
    }
  },
  methods: {
    /* 获取区域列表的回调函数 */
    async fetchAreaList() {
      try {
        this.loading = true
        await this.$store.dispatch('area/fetchAreaList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },

    /* 	pageSize改变时触发的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchAreaList()
    },
    /* currentPage 改变时触发的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchAreaList()
    },
    /* 对话框关闭时的回调函数 */
    handleDialogClose() {
      this.$refs.areaForm.resetForm()
    },
    /* 点击添加按钮的回调函数 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 点击编辑按钮的回调函数 */
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.areaForm.findArea(row)
      })
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.areaName}, 是否继续?`, '提示', { type: 'warning' }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('area/deleteArea', row.id)
        await this.fetchAreaList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      const valida = await this.$refs.areaForm.validateForm()
      if (!valida) return
      if (this.edit) {
        await this.$refs.areaForm.editArea()
      } else {
        await this.$refs.areaForm.addArea()
      }
      this.dialogVisible = false
     await this.fetchAreaList()
    }
  },
  computed: {
    ...mapGetters('area', ['areaList', 'total']),
    title() {
      return this.edit ? '编辑区域' : '添加区域'
    }
  },
  created() {
    this.fetchAreaList()
  }
}
</script>

<template>
  <div class="area">
    <el-card>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加区域</el-button>
    </el-card>
    <el-card>
      <el-table size="small" border stripe :header-cell-style="{background:'#4d8ed9',color:'#fff'}" :data="areaList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column label="序号" align="center" type="index"></el-table-column>
        <el-table-column label="区域名称" align="center" prop="areaName"></el-table-column>
        <el-table-column label="车位数（个）" align="center" prop="spaceNumber"></el-table-column>
        <el-table-column label="面积（㎡）" align="center" prop="areaProportion"></el-table-column>
        <el-table-column label="计费规则" align="center" prop="ruleName"></el-table-column>
        <el-table-column label="规则详情" align="center" prop="hoverRuleName" show-overflow-tooltip></el-table-column>
        <el-table-column label="备注" align="center" prop="remark"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="pageParams.page" :page-sizes="[20, 30, 40,50]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total" background
      >
      </el-pagination>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%" @close="handleDialogClose">
      <area-form ref="areaForm"></area-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<style scoped lang="scss">
.area {
  .el-card {
    margin-bottom: 10px;
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }

  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
  }
}
</style>
